import { ref, computed } from "vue";
import {defineStore} from 'pinia'
import {
  addToCartAPI,
  loadCartAPI,
  delFromCartAPI,
} from "../../services/shop-carts";

export const useCart = defineStore('cart',()=>{
  const carts = ref([])

  //加入购物车
  const addToCart = async(productId,amount=1,price='')=>{
    await addToCartAPI({
      product:productId,amount,price
    })
    loadData()
  }
  
  //删除购物车数据
  const delFromCart = async(id)=>{
    await delFromCartAPI(id)
    loadData()
  }

  //获取购物车数据
  const loadData = async()=>{
    const res = await loadCartAPI()
    carts.value =res.data
  }

  return{
    carts,addToCart,delFromCart,loadData
  }
})

